package com.xyzh05.webapp.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.xyzh05.util.StringUtil;

public class UploadFileAction extends BaseAction {
	private static final long serialVersionUID = 2274302051220170418L;

	protected static final int BUFFER_SIZE = 16 * 1024;
	// 文件标题
	public String title;
	
	// 用File数组来封装多个上传文件域对象
	protected File[] upload;
	
	// 用String数组来封装多个上传文件名
	protected String[] uploadFileName;
	
	// 用String数组来封装多个上传文件类型
	protected String[] uploadContentType;
	
	// 保存文件的目录路径(通过依赖注入)
	protected String savePath;
	
	//文件大小
	protected Integer[] fileSize;
	
	//文件存储绝对路径
	protected String[] targetPath;	

	// 自己封装的一个把源文件对象复制成目标文件对象
	private static boolean copy(File src, File dst) {
		boolean result = false;
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public List<String> uploadFiles() throws Exception {
		File[] srcFiles = this.getUpload();
		targetPath = new String[10];
		fileSize = new Integer[10];
		List<String> successFileList = new ArrayList<String>();
		// 处理每个要上传的文件
		for (int i = 0; i < srcFiles.length; i++) {
			// 根据服务器的文件保存地址和原文件名创建目录文件全路径
			String dstPath = ServletActionContext.getServletContext().getRealPath(getSavePath()) + "\\" + StringUtil.getEntry();
			targetPath[i] = dstPath;
			File target = new File(dstPath);
			if(!target.isDirectory()){
				new File(this.getSavePath()).mkdir();
			}				
			if (copy(srcFiles[i], target)) {
				fileSize[i] = this.getFileSize(target); 
				successFileList.add(getUploadFileName()[i]);
			}
		}
		getRequest().setAttribute("successFileList", successFileList);
		return successFileList;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String[] getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public Integer[] getFileSize() {
		return fileSize;
	}

	public void setFileSize(Integer[] fileSize) {
		this.fileSize = fileSize;
	}

	public String[] getTargetPath() {
		return targetPath;
	}

	public void setTargetPath(String[] targetPath) {
		this.targetPath = targetPath;
	}
	
}